# pb文件生成

QUIET ?= @
PROJECT_ROOT := $(shell pwd)
PROTO_DIR := $(PROTOCOL_BUFFERS)/proto
OUTPUT_DIR := $(PROJECT_ROOT)/pkg
BUF := buf
PROTO_FORMATTER := buf format

.PHONY: all proto generate clean help install-deps lint deps

all: proto

proto: generate

generate: deps
	@$(BUF) generate
	@if [ -d "pkg/proto" ]; then \
		shopt -s dotglob; \
		cp -r pkg/proto/* pkg/ 2>/dev/null || true; \
		shopt -u dotglob; \
		rm -rf pkg/proto; \
	fi

deps:
	@echo "Updating dependencies..."
	@$(BUF) mod update

clean:
	@echo "Cleaning generated protobuf files..."
	@find $(OUTPUT_DIR) -name "*.pb.go" -delete
	@find $(OUTPUT_DIR) -name "*_grpc.pb.go" -delete
	@echo "Clean completed!"

install-deps:
	@echo "Installing protobuf tools..."
	@go install github.com/bufbuild/buf/cmd/buf@latest
	@go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
	@go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
	@go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest
	@echo "Tools installed successfully!"

list-proto:
	@echo "Proto files found:"
	@find $(PROTO_DIR) -name "*.proto" | while read file; do \
		echo "  $$file"; \
	done

verify: generate
	@echo "Verifying generated files..."
	@if [ -z "$(shell find $(OUTPUT_DIR) -name "*.pb.go" -print -quit)" ]; then \
		echo "Error: No .pb.go files generated!"; \
		exit 1; \
	else \
		echo "✓ Protobuf files generated successfully!"; \
	fi

lint:
	@echo "Linting proto files..."
	@$(BUF) lint

format:
	@echo "Formatting proto files..."
	@$(BUF) format -w

.PHONY: help
help:
	@echo "Protobuf Generation Makefile"
	@echo ""
	@echo "Available targets:"
	@echo "  all          - Alias for 'proto'"
	@echo "  proto        - Alias for 'generate'"
	@echo "  generate     - Generate protobuf Go code using buf"
	@echo "  deps         - Update proto dependencies"
	@echo "  clean        - Clean generated protobuf files"
	@echo "  install-deps - Install required tools"
	@echo "  list-proto   - List all proto files"
	@echo "  verify       - Generate and verify output"
	@echo "  lint         - Lint proto files"
	@echo "  format       - Format proto files"
	@echo "  help         - Show this help message"